package com.wfuhui.modules.store.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wfuhui.modules.member.entity.MemberEntity;
import com.wfuhui.modules.store.dao.CouponDao;
import com.wfuhui.modules.store.dao.UserCouponDao;
import com.wfuhui.modules.store.entity.CouponEntity;
import com.wfuhui.modules.store.entity.UserCouponEntity;
import com.wfuhui.modules.store.service.CouponService;


@Service("couponService")
public class CouponServiceImpl implements CouponService {
	@Autowired
	private CouponDao couponDao;
	@Autowired
	private UserCouponDao userCouponDao;
	
	@Override
	public CouponEntity queryObject(Integer id){
		return couponDao.queryObject(id);
	}
	
	@Override
	public List<CouponEntity> queryList(Map<String, Object> map){
		return couponDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return couponDao.queryTotal(map);
	}
	
	@Override
	@Transactional
	public void save(CouponEntity coupon){
		couponDao.save(coupon);
		List<UserCouponEntity> userCouponList = new ArrayList<UserCouponEntity>();
		for (MemberEntity user : coupon.getUserList()) {
			UserCouponEntity userCoupon = new UserCouponEntity();
			userCoupon.setCouponId(coupon.getId());
			userCoupon.setUserId(user.getId());
			userCoupon.setStatus(1);
			userCouponList.add(userCoupon);
		}
		userCouponDao.saveBatch(userCouponList);
	}
	
	@Override
	public void update(CouponEntity coupon){
		couponDao.update(coupon);
	}
	
	@Override
	public void delete(Integer id){
		couponDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		couponDao.deleteBatch(ids);
		userCouponDao.deleteBatchByCouponIds(ids);
	}
	
}
